import React, { useState } from 'react'
import { Form, Input, Button } from 'antd'
import {UserOutlined, LockOutlined,} from '@ant-design/icons'
import { loginApi, getMenusApi } from '../apis/user'
import { useNavigate } from 'react-router-dom'
export default function Login() {
    const navigate = useNavigate();
    const [loading, setLoading] = useState(false)
    async function onFinish(values){          
        const res = await loginApi(values)
        console.log(res);
        setLoading(false)
        //登录成功，1. 把用户数据存储到本地存储 
        const { user, token, permissions, roles } = res.data;
        localStorage.user = JSON.stringify(user);
        localStorage.token = token;
        localStorage.permissions = JSON.stringify(permissions);
        localStorage.roles = JSON.stringify(roles);
        //2. 跳转到导航页
        navigate("/home",)
    }
    function onFinishFailed(){}
    return (
        <div className='f-c-c h100vh' >
            <div>
                <img src="http://xawn.f3322.net:8002/distremote/static/img/logo.png" alt="" />
                <h2 style={{margin:'0 0 50px 20px',float:'right',fontWeight:'bold'}}>赤兔养车</h2>
            </div>
            <Form
                name="basic"
                onFinish={onFinish}
                onFinishFailed={onFinishFailed}
                autoComplete="off"
            >
                <Form.Item
                    
                    name="username"
                    rules={[{ required: true, message: '请输入账号!' }]}
                >
                    <Input prefix={<UserOutlined className="site-form-item-icon" />}/>
                </Form.Item>

                <Form.Item
                   
                    name="password"
                    rules={[{ required: true, message: '请输入密码!' }]}
                >
                    <Input.Password prefix={<LockOutlined className="site-form-item-icon" />}/>
                </Form.Item>

               
                <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
                    <Button type="primary" htmlType="submit" loading={loading}className="login">
                        登录
                    </Button>
                </Form.Item>
            </Form>
        </div>
    )
}
